package com.lulu.permissioncheckproject;

import static android.content.Context.*;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import androidx.core.app.ActivityCompat;

import java.lang.reflect.Method;
import java.net.NetworkInterface;
import java.net.SocketException;

/**
 * @author zhanglulu
 */
public class TestRequestPermission {
  private static final String TAG = "RequestPermission";

  public static void tryGetPermission(Context context) {
    LocationManager location = (LocationManager) context.getSystemService(LOCATION_SERVICE);
    location.getAllProviders();
    context.getSystemService(KEYGUARD_SERVICE);
    WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    String macAddress = wifiInfo.getMacAddress();
    Log.d(TAG, "macAddress: " + macAddress);

    String imei = "";
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
    try {
      Method method = telephonyManager.getClass().getMethod("getImei");
      imei = (String) method.invoke(telephonyManager);
    } catch (Exception e) {
      e.printStackTrace();
    }

    Log.d(TAG, "tryGetPermission: getImei: " + imei);

    String deviceID = null;
    try {
      deviceID = telephonyManager.getDeviceId();
    } catch (Exception e) {
      e.printStackTrace();
    }
    Log.d(TAG, "tryGetPermission: getDeviceId: " + deviceID);
    NetworkInterface networkInterface = null;
    try {
      networkInterface = NetworkInterface.getByName("wlan0");
      byte[] mac = networkInterface.getHardwareAddress();
      Log.d(TAG, "networkInterface.getHardwareAddress " + mac);
    } catch (Exception e) {
      e.printStackTrace();
    }

    try {
      String subscriberId = telephonyManager.getSubscriberId();
      Log.d(TAG, "tryGetPermission: subscriberId: " + subscriberId);
    } catch (Exception e) {
      e.printStackTrace();
    }

    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
      && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
      Location net = location.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
      Location gps = location.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    }

  }
}
